package com.pigcloud.sop.gateway.config;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsx
 * @date 2023/12/5 17:06
 */
@Data
@ConfigurationProperties(prefix = "gateway.ignore.auth")
@Component
public class IgnoreAuthConfig {
    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();

    private List<String> baseUri = CollUtil.newArrayList(
            "/**/*.css",
            "/**/*.js",
            "/**/*.html",
            "/**/*.ico",
            "/**/*.jpg",
            "/**/*.jpeg",
            "/**/*.png",
            "/**/*.gif",
            "/**/api-docs/**",
            "/**/api-docs-ext/**",
            "/**/swagger-resources/**",
            "/**/webjars/**",
            "/actuator/**",
            "/**/static/**",
            "/**/public/**",
            "/error",
            "/**/doc.html",
            "/**/swagger-ui.html",
            // api 扫描
            "/*/systemApiScan",
            // 文件服务OSS 回调
            "/nk/oss-policy-callback/**",
            // 登录验证码
            "/nk/captcha/**",
            // 登录短信
            "/nk/sms/**",
            // 忘记密码
            "/nk/forget-password/**",
            "/oauth2/token"

    );

    /**
     * 不需要登录
     */
    private List<String> token = CollUtil.newArrayList("/yk/platform/**");

    public boolean isIgnoreToken(String path) {
        List<String> all = new ArrayList<>();
        all.addAll(getBaseUri());
        all.addAll(getToken());
        return all.stream().anyMatch(url -> path.startsWith(url) || ANT_PATH_MATCHER.match(url, path));
    }

}
